'use strict'
import config from './confing.js'
export default {
	request(url, method, data, noLoading, isJson) {
		this.request.interceptor = (config) => {
			console.log(111111111111,config)
		} 
		if (!noLoading) {
			uni.showLoading({
				title: "正在加载..."
			});
		}
		// 合并请求参数
		if (Array.isArray(data)) {
			sendData = data;
		} else {
			sendData = { ...data,
				...config.configData
			}
		}
		let requestId = new Date().getTime();
		// config.reqlog(url,method,sendData,requestId)
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.host() + url,
				method: method,
				data: sendData,
				header: config.getHeader(method, isJson),
				success: (res) => {
					if (res.data.meta.success) {
						uni.hideLoading();
						resolve(res.data.data)
					} else {
						config.showToast(res.data.meta.message, 'none', 1000)
						reject(res)
					}
				},
				fail: (res) => {
					config.showToast('请求失败！', 'none', 1000)
					uni.hideLoading();
				},
				complete: (res) => {
					uni.hideLoading()
					res.requestId = requestId
					// config.reslog(res)
				},
			})
		})
	},
	get(reqParams) {
		let noLoading = reqParams.noLoading ? reqParams.noLoading : false
		return this.request(reqParams.url, 'GET', reqParams.data, noLoading)
	},
	post(reqParams) {
		let noLoading = reqParams.noLoading ? reqParams.noLoading : false
		let isJson = reqParams.isJson ? reqParams.isJson : false
		return this.request(reqParams.url, 'POST', reqParams.data, noLoading, isJson)
	},
	uploadFileAjax(url, filePath, data, fileName) {
		uni.showLoading({
			title: "正在加载..."
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: uploadFileUlr + url,
				header: getHeader(),
				filePath: filePath,
				name: encodeURI(fileName),
				formData: data,
				success: (res) => {
					if (res.statusCode == 200) {
						uni.hideLoading();
						resolve(res.data)

					} else {
						uni.hideLoading();
						config.showToast(res.data.meta.message, 'none', 1000)
						reject(res)
					}
				},
				fail: function(res) {
					uni.hideLoading();
				},
			});
		})
	},
	uploadFile(reqParams) {
		let noLoading = reqParams.noLoading ? reqParams.noLoading : false
		return this.uploadFileAjax(reqParams.url, reqParams.filePath, reqParams.data, reqParams.name, noLoading)
	},
}
